//package com.eastfair.transaction.mq.listener;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.eastfair.mq.model.MySimpleMessage;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//
//import java.util.Map;
//
///**
// * @Description: 一个项目有且仅有一个@RocketMQTransactionListener注解
// * @author: ligang
// * @date: 2022年05月01日 17:26
// */
//@Slf4j
//@RocketMQTransactionListener
//public class GlobalTransactionListner implements RocketMQLocalTransactionListener {
//
////    @Autowired
////    private OrderService orderService;
////    @Autowired
////    private StoreService storeService;
//    /**
//     * 执行本地事务
//     */
//    @Override
//    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
//        try {
//            //执行本地事务
//            MySimpleMessage mySimpleMessage = JSONObject.parseObject(new String((byte[]) msg.getPayload()), MySimpleMessage.class);
//            String tag = mySimpleMessage.getHeaders().get("tag").toString();
////            if (PointKeyDefinition.TRANSACTION_ORDER.equals(tag)) {
////                OrderTransaction orderTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(),OrderTransaction.class);
////                log.info(JSON.toJSONString(orderTransaction));
////                //执行本地事务
////                orderService.executeCommit(orderTransaction);
////                return RocketMQLocalTransactionState.COMMIT;
////            }else  if (PointKeyDefinition.TRANSACTION_STORE.equals(tag)) {
////                StoreTransaction storeTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(),StoreTransaction.class);
////                log.info(JSON.toJSONString(storeTransaction));
////                //执行本地事务
////                return storeService.executeCommit(storeTransaction);
////            }
//            return RocketMQLocalTransactionState.COMMIT;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return RocketMQLocalTransactionState.ROLLBACK;
//        }
//    }
//
//    /**
//     * 本地事务的检查，检查本地事务是否成功
//     */
//    @Override
//    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
//        MySimpleMessage mySimpleMessage = JSONObject.parseObject(new String((byte[]) msg.getPayload()), MySimpleMessage.class);
//        String tag = mySimpleMessage.getHeaders().get("tag").toString();
////        if (PointKeyDefinition.TRANSACTION_ORDER.equals(tag)) {
////            OrderTransaction orderTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(),OrderTransaction.class);
////            log.info(JSON.toJSONString(orderTransaction));
////            return orderService.backTransaction(orderTransaction);
////        }else if (PointKeyDefinition.TRANSACTION_STORE.equals(tag)) {
////            StoreTransaction storeTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(),StoreTransaction.class);
////            log.info(JSON.toJSONString(storeTransaction));
////            return storeService.backTransaction(storeTransaction);
////        }
//        return RocketMQLocalTransactionState.COMMIT;
//    }
//
//}
